package com.cskaoyan.service.admin.brand;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.vo.admin.brand.BrandListVo;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/07/9:08
 * @Description: 品牌制造商
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;

    /**
    * @Author: 宾文杰 @Description: 根据当前页面、限制数和排序条件获取全部品牌制造商信息 @Date: 2022/9/7
    */
    @Override
    public BrandListVo brandList(Integer page, Integer limit, String sort, String order,Integer id,String name) {
        // 执行查询之前 → 使用PageHelper开启分页就可以了
        PageHelper.startPage(page,limit);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setDistinct(true);
        //根据sort排序
        marketBrandExample.setOrderByClause(sort + " " + order);
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        //根据id和name查找
        if (id!=null){
            criteria.andIdEqualTo(id);
        }
        if (name!=null&& !StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo pageInfo = new PageInfo(marketBrands);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        BrandListVo brandListVo = new BrandListVo();
        brandListVo.setLimit(limit);
        brandListVo.setPage(page);
        brandListVo.setPages(pages);
        brandListVo.setTotal(total);
        brandListVo.setList(marketBrands.toArray());
        return brandListVo;
    }

    /**
    * @Author: 宾文杰 @Description: 添加品牌商信息 @Date: 2022/9/7
    */
    @Override
    public MarketBrand brandCreate(MarketBrand marketBrand) {
        //生成当前时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(new Date());
        try {
            Date addTime = dateFormat.parse(format);
            marketBrand.setAddTime(addTime);
            marketBrand.setUpdateTime(addTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //先添加品牌商信息，自动返回自增id
        marketBrandMapper.insertSelective(marketBrand);
        Integer id = marketBrand.getId();
        //根据id获取品牌商信息
        MarketBrand marketBrand1 = marketBrandMapper.selectByPrimaryKey(id);
        return marketBrand1;
    }

    /**
    * @Author: 宾文杰 @Description: 更新品牌商信息 @Date: 2022/9/7
    */
    @Override
    public MarketBrand brandUpdate(MarketBrand marketBrand) {

        marketBrandMapper.updateByPrimaryKey(marketBrand);
        MarketBrand marketBrand1 = marketBrandMapper.selectByPrimaryKey(marketBrand.getId());
        return marketBrand1;
    }

    /**
    * @Author: 宾文杰 @Description: 删除品牌商信息 @Date: 2022/9/7
    */
    @Override
    public void brandDelete(MarketBrand marketBrand) {
        marketBrandMapper.deleteByPrimaryKey(marketBrand.getId());
    }
}
